#include "Queue.h"
 
extern const uint8_t CENTER_X = 64;
extern const uint8_t CENTER_Y = 32;
extern const uint8_t RADIUS = 30;
#define QUEUE_CRITICAL_ENTER() __disable_irq()
#define QUEUE_CRITICAL_EXIT() __enable_irq()

void Queue_Init(CircularQueue *q){
    QUEUE_CRITICAL_ENTER();
    q->head = 0; 
    q->tail = 0; 
    q->count = 0;
    QUEUE_CRITICAL_EXIT();

}

//判断队列是否为空
bool Queue_isEmpty(const CircularQueue *q){ 
    return (q->count == 0);

}
//判断队列是否为满
bool Queue_isFull(const CircularQueue *q){
    return (q->count >= Queue_Capacity);
}

//入队
bool Queue_EnQueue(CircularQueue *q,uint8_t val){
    QUEUE_CRITICAL_ENTER();
    
    if(Queue_isFull(q)) return false;
    q->data[q->tail] = val;
    q->tail = (q->tail + 1) % Queue_Capacity;
    q->count++;

    QUEUE_CRITICAL_EXIT();
    return true;
    

}
//出队
bool Queue_DeQueue(CircularQueue *q,uint8_t *val){
    QUEUE_CRITICAL_ENTER();
    if(Queue_isEmpty(q)){
        QUEUE_CRITICAL_EXIT();
        return false;
    }
    
    *val = q->data[q->head];
    q->head = (q->head + 1) % Queue_Capacity;
    q->count--;
    QUEUE_CRITICAL_EXIT();
    return true;
}

// void OLED_showRotatingQueue(const CircularQueue *q,float angle){
  
//   OLED_NewFrame();
//   OLED_DrawCircle(CENTER_X,CENTER_Y,RADIUS,OLED_COLOR_NORMAL);
//   for(uint8_t i = 0;i<q->count;i++){
//   float itemAngle = angle + (i*(360.0f / q->count));
//   uint8_t x = CENTER_X + RADIUS *cosf(itemAngle * M_PI / 180);
//   uint8_t y = CENTER_Y + RADIUS *sinf(itemAngle * M_PI / 180);
//   char num_str[2] = {'0' + q->data[(q->head + i)%Queue_Capacity],'\0'};
//   OLED_PrintASCIIString(x-3,y-3,num_str,&afont16x8,OLED_COLOR_NORMAL);
//  }
//  OLED_ShowFrame();
// }
